// 25. K个一组翻转链表
package Leetcode_Second_Brush;

public class Solution025 {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode node = head;
        int length = 0;
        while (node != null) {
            node = node.next;
            length++;
        }
        ListNode newHead = new ListNode();
        node = newHead;
        int times = length / k;
        while (times-- != 0) {
            for (int i = 0; i < k; i++) {
                ListNode next = head.next;
                head.next = node.next;
                node.next = head;
                head = next;
            }
            while (node.next != null)
                node = node.next;
        }
        node.next = head;
        return newHead.next;
    }
}
